#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
int a[MAXN];
void solve(){
	int n;
	string s;
	cin >> n;
	cin >> s;
	if(s.find("pie") == -1 && s.find("map") == -1){
		cout << "0" << "\n";
		return ;
	}
	int cnt = 0;
	while(s.find("mapie") != -1){
		int idx = s.find("mapie");
		s.erase(idx + 2,1);
		cnt++;
	}
	while(s.find("pie") != -1){
		int idx = s.find("pie");
		s.erase(idx + 1,1);
		cnt++;
	}
	while(s.find("map") != -1){
		int idx = s.find("map");
		s.erase(idx + 1,1);
		// cout << s << "\n";
		cnt++;
	}
	cout << cnt << "\n";
}
int main(){
	ios::sync_with_stdio(false),cin.tie(0);
	int t;
	cin >> t;
	while(t--) solve();
}